#!/bin/bash

# 登录界面性能分析脚本
# 用于深入分析代码结构和性能瓶颈

echo "🔍 开始登录界面性能代码分析..."
echo "==========================================="
echo ""

# 分析LoginUIConfig配置
echo "📋 1. 分析LoginUIConfig配置"
echo "----------------------------"
if [ -f "CMB250716/Config/LoginUIConfig.swift" ]; then
    echo "✅ 找到LoginUIConfig.swift文件"
    echo ""
    echo "当前防抖延迟配置:"
    grep -n "textFieldDebounceDelay" CMB250716/Config/LoginUIConfig.swift || echo "❌ 未找到textFieldDebounceDelay配置"
    echo ""
    echo "当前动画配置:"
    grep -n "AnimationDuration\|TransitionDuration" CMB250716/Config/LoginUIConfig.swift || echo "❌ 未找到动画配置"
    echo ""
    echo "当前反馈延迟配置:"
    grep -n "FeedbackDelay\|ResponseDelay" CMB250716/Config/LoginUIConfig.swift || echo "❌ 未找到反馈延迟配置"
else
    echo "❌ 未找到LoginUIConfig.swift文件"
fi
echo ""

# 分析LoginView中的onChange处理
echo "📱 2. 分析LoginView中的onChange处理"
echo "----------------------------------"
if [ -f "CMB250716/Views/LoginView.swift" ]; then
    echo "✅ 找到LoginView.swift文件"
    echo ""
    echo "电话号码输入框的onChange事件:"
    grep -A 10 -B 2 "onChange.*phoneNumber" CMB250716/Views/LoginView.swift || echo "❌ 未找到phoneNumber的onChange处理"
    echo ""
    echo "验证码输入框的onChange事件:"
    grep -A 10 -B 2 "onChange.*verificationCode" CMB250716/Views/LoginView.swift || echo "❌ 未找到verificationCode的onChange处理"
else
    echo "❌ 未找到LoginView.swift文件"
fi
echo ""

# 分析动画相关代码
echo "🎬 3. 分析动画相关代码"
echo "----------------------"
echo "搜索所有动画相关代码:"
grep -r -n "withAnimation\|animation(\|Animation\|transition" CMB250716/Views/ --include="*.swift" | head -20
echo ""

# 分析触觉反馈代码
echo "📳 4. 分析触觉反馈代码"
echo "----------------------"
echo "搜索触觉反馈相关代码:"
grep -r -n "HapticFeedback\|UIImpactFeedback\|haptic" CMB250716/Views/ --include="*.swift" | head -10
echo ""

# 分析状态管理
echo "🔄 5. 分析状态管理"
echo "------------------"
echo "搜索状态变量定义:"
grep -r -n "@State\|@StateObject\|@ObservedObject\|@Published" CMB250716/Views/LoginView.swift | head -15
echo ""

# 分析性能相关的Timer和延迟
echo "⏱️ 6. 分析Timer和延迟处理"
echo "-------------------------"
echo "搜索Timer和延迟相关代码:"
grep -r -n "Timer\|DispatchQueue\|asyncAfter\|delay" CMB250716/Views/ --include="*.swift" | head -15
echo ""

# 生成性能分析总结
echo "📊 7. 性能分析总结"
echo "------------------"
echo "基于代码分析的发现:"
echo ""

# 检查是否存在性能问题的模式
echo "🔍 潜在性能问题检测:"

# 检查复杂的onChange处理
ONCHANGE_COUNT=$(grep -r "onChange" CMB250716/Views/LoginView.swift | wc -l)
echo "- onChange事件数量: $ONCHANGE_COUNT 个"
if [ $ONCHANGE_COUNT -gt 5 ]; then
    echo "  ⚠️  警告: onChange事件过多，可能影响性能"
else
    echo "  ✅ onChange事件数量正常"
fi

# 检查动画数量
ANIMATION_COUNT=$(grep -r "withAnimation\|animation(" CMB250716/Views/LoginView.swift | wc -l)
echo "- 动画调用数量: $ANIMATION_COUNT 个"
if [ $ANIMATION_COUNT -gt 10 ]; then
    echo "  ⚠️  警告: 动画过多，可能影响流畅度"
else
    echo "  ✅ 动画数量合理"
fi

# 检查状态变量数量
STATE_COUNT=$(grep -r "@State" CMB250716/Views/LoginView.swift | wc -l)
echo "- @State变量数量: $STATE_COUNT 个"
if [ $STATE_COUNT -gt 20 ]; then
    echo "  ⚠️  警告: 状态变量过多，可能影响重绘性能"
else
    echo "  ✅ 状态变量数量合理"
fi

echo ""
echo "🎯 优化建议优先级:"
echo "1. 🔴 高优先级: 减少输入防抖延迟 (textFieldDebounceDelay)"
echo "2. 🟡 中优先级: 优化onChange事件处理链"
echo "3. 🟢 低优先级: 统一动画配置和减少并发动画"
echo ""
echo "📋 详细分析报告已生成: 登录界面性能诊断报告.md"
echo "🛠️  性能测试工具: 长按应用底部版权信息2秒触发"
echo ""
echo "分析完成！ ✨"